﻿Imports System.Drawing.Brush

Public Class ucGuage

    Private _Speed As Double = 0
    Public Property Speed() As Double
        Get
            Return _Speed
        End Get
        Set(ByVal value As Double)
            _Speed = value
            picGuage.Refresh()
        End Set
    End Property


    Private _SatCount As Integer = 0
    Public Property SatCount() As Integer
        Get
            Return _SatCount
        End Get
        Set(ByVal value As Integer)
            _SatCount = value
        End Set
    End Property

    Private Sub picGuage_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles picGuage.Paint
        Draw(e.Graphics, Me.Speed)
    End Sub

    Private Sub Draw(ByVal objG As Graphics, ByVal Speed As Double)
        Select Case Me.SatCount
            Case 0
                objG.DrawImage(My.Resources.sat_0, 301, 194)
            Case 1
                objG.DrawImage(My.Resources.sat_1, 301, 194)
            Case 2
                objG.DrawImage(My.Resources.sat_2, 301, 194)
            Case 3
                objG.DrawImage(My.Resources.sat_3, 301, 194)
            Case 4
                objG.DrawImage(My.Resources.sat_4, 301, 194)
            Case 5
                objG.DrawImage(My.Resources.sat_5, 301, 194)
            Case 6
                objG.DrawImage(My.Resources.sat_6, 301, 194)
            Case 7
                objG.DrawImage(My.Resources.sat_7, 301, 194)
            Case Else
                objG.DrawImage(My.Resources.sat_8, 301, 194)
        End Select

        Dim P As Point = GetXY(Speed)
        Dim Pn As New Pen(Color.Yellow, 10)
        objG.DrawLine(Pn, P.X, P.Y, 230, 230)

        Dim StringBrush As Brush = New SolidBrush(Color.Yellow)
        Dim SpeedString As String = Speed.ToString("0#.0")
        Dim pSize As SizeF = objG.MeasureString(SpeedString, Me.Font)
        Dim SX As Double = 235 - pSize.Width / 2
        objG.DrawString(SpeedString, Me.Font, StringBrush, SX, 350)
    End Sub

    Private Function GetXY(ByVal Speed As Double) As Point
        Dim Angle As Double = GetAngle(Speed)
        Dim D_Angle As Double = 180 - Angle
        D_Angle = D_Angle * (Math.PI / 180)

        Dim X1 As Double = 230
        Dim Y1 As Double = 230
        Dim L As Double = 170

        Dim X As Double = X1 - L * Math.Sin(D_Angle)
        Dim Y As Double = Y1 - L * Math.Cos(D_Angle)
        Dim rtnVal As New Point(CInt(X), CInt(Y))
        Return rtnVal
    End Function

    Private Function GetAngle(ByVal Speed As Double) As Double
        Dim A1 As Double = 31
        Dim A2 As Double = 245
        Dim rtnVal As Double
        rtnVal = ((A2 - A1) / 200) * Speed + A1
        Return rtnVal
    End Function
End Class
